package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/robfig/cron/v3"
	"tao-admin/configs"
	"tao-admin/internal/api/bill_management"
	"tao-admin/internal/api/duty_info"
	"tao-admin/internal/api/notes"
	"tao-admin/internal/api/todo_plan"
	"tao-admin/internal/api/users"
	"tao-admin/internal/api/version_info"
	"tao-admin/internal/router"
)

func main() {
	// 1、初始化配置
	configs.InitConf()

	// 2、初始化DB
	configs.InitDB()
	defer configs.CloseDB()

	// 3、定时任务 Create a new cron instance.
	c := cron.New()
	router.InitCron(c)
	c.Start()
	defer c.Stop()

	// 4、业务注册
	//e := gin.Default()
	//router.InitApps(e)

	router.Include(
		//version_info.Routers,  //老的路由返回式
		version_info.ViewInit, //新的路由方式
		duty_info.Routers,
		duty_info.ViewInit,
		//users.Routers, //用户登录
		users.UserViewInit,
		todo_plan.PlanViewInit,
		notes.NoteViewInit,
		bill_management.BillViewInit,
	)
	r := router.Init()
	//未知路由处理
	r.NoRoute(func(content *gin.Context) {
		content.String(404, "No router!")
	})
	//未知调用方式处理
	r.NoMethod(func(content *gin.Context) {
		content.String(404, "Not method!")
	})
	r.Group("/api")

	// 5、运行
	if err := r.Run(fmt.Sprintf(":" + configs.AppConfig.ServerPort)); err != nil {
		fmt.Printf("startup service failed, err: %v\n", err)
	}

}
